<div class="layui-fluid layui-col-md12" lay-title="编辑友链分类">
	<div class="layui-card">
		<div class="layui-card-body" style="padding: 15px;">
			<form class="layui-form" action="" lay-filter="form-group">
				<input type="text" name="id" style="display:none">
				<div class="layui-form-item">
					<label class="layui-form-label">分类标题</label>
					<div class="layui-input-block">
						<input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入标题(20个字符内)" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">是否公开</label>
					<div class="layui-input-block">
						<input type="checkbox" name="status" lay-skin="switch" lay-text="ON|OFF">
					</div>
				</div>
				<div class="layui-form-item layui-form-text">
					<label class="layui-form-label">简单备注</label>
					<div class="layui-input-block">
						<textarea name="texts" id="texts" placeholder="请输入备注内容" class="layui-textarea"></textarea>
					</div>
				</div>
				<div class="layui-form-item">
					<div class="layui-input-block">
						<button class="layui-btn" lay-submit="" lay-filter="links-submit">立即提交</button>
						<button type="reset" class="layui-btn layui-btn-primary">重置</button>
					</div>
				</div>
			</form>
		</div>
	</div>
</div>

<script>
	layui.use(['admin', 'form', 'laydate'], function() {
		var $ = layui.$,
			admin = layui.admin,
			element = layui.element,
			layer = layui.layer,
			laydate = layui.laydate,
			router = layui.router(),
			form = layui.form;
		form.render(null, 'form-group');
		// 拉取友链详情
		if (router.search.id) {
			admin.get({
				url: layui.api.links + 'get?id=' + router.search.id,
				async: false,
				success: function(result) {
					let res = result.data;
					let status = res.status;
					$('input[name="id"]').val(res.id);
					if (status == 1) {
						$("input[name='status']").prop("checked", true);
					} else {
						$("input[name='status']").prop("checked", false);
					}
					$('input[name="title"]').val(res.title);
					$('#texts').val(res.texts);
					form.render(null, 'form-group');
				}
			});
		} else {
			layui.view.setTitle('新增友链分类', router.href);
		}

		/* 自定义验证规则 */
		form.verify({
			title: function(value) {
				if (value.length < 1) {
					return '标题至少得1个字符啊';
				}
			}
		});

		/* 监听提交 */
		form.on('submit(links-submit)', function(data) {
			/** 屏蔽回车 **/
			var theEvent = window.event;
			var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
			if (code == 13) {
				return false;
			}
			let datas = data.field;
			if (!datas.status) {
				datas.status = 2;
			} else {
				datas.status = 1;
			}
			var time = Math.round(new Date().getTime() / 1000).toString();
			
			if (!router.search.id) {
                datas.cdat = time;
	        }

			// 这一段是用于判断是否有相同的值的
			datas.onlykey = 'title';
			datas.onlyname = datas.title;
			admin.post({
				url: layui.api.links + 'save',
				type: 'post',
				data: datas,
				success: function(result) {
					layer.alert(result.msg, {
						title: '提交提示'
					}, function() {
						// 这里加上时间戳,刷新页面
						admin.navigate('/list/links/time=' + time);
					});

				}
			});
			return false;
		});
	});
</script>
